//
//  Dock.m
//  新浪微博
//
//  Created by apple on 13-10-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Dock.h"
#import "DockItem.h"

@interface Dock()
{
    NSMutableArray* _items;
}
@end

@implementation Dock


-(id)init {
    self = [super init];
    if(self) {
        _items = [NSMutableArray array];
        _itemSelectable = YES;
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        _items = [NSMutableArray array];
        _itemSelectable = YES;
    }
    return self;

}

- (void)addDockItemWithTitle:(NSString *)title titleColorForNormal:(UIColor*)titleColorForNormal titleColorForSelected:(UIColor*)titleColorForSelected iconForNormal:(NSString*)iconForNormal iconForSelected:(NSString *)iconForSelected {
    [self addDockItemWithTitle:title titleColorForNormal:titleColorForNormal titleColorForHighlighted:nil titleColorForSelected:titleColorForSelected titleColorForDisabled:nil iconForNormal:iconForNormal iconForHighlighted:nil iconForSelected:iconForSelected iconForDisabled:nil];
}

- (void)addDockItemWithTitle:(NSString *)title titleColorForNormal:(UIColor*)titleColorForNormal titleColorForHighlighted:(UIColor*)titleColorForHighlighted iconForNormal:(NSString*)iconForNormal iconForHighlighted:(NSString *)iconForHighlighted {
    
    [self addDockItemWithTitle:title titleColorForNormal:titleColorForNormal titleColorForHighlighted:titleColorForHighlighted titleColorForSelected:nil titleColorForDisabled:nil iconForNormal:iconForNormal iconForHighlighted:nil iconForSelected:iconForNormal iconForDisabled:nil];
}


- (void)addDockItemWithTitle:(NSString *)title titleColorForNormal:(UIColor*)titleColorForNormal titleColorForHighlighted:(UIColor*)titleColorForHighlighted titleColorForSelected:(UIColor*)titleColorForSelected iconForNormal:(NSString*)iconForNormal iconForHighlighted:(NSString *)iconForHighlighted iconForSelected:(NSString *)iconForSelected {
    
    [self addDockItemWithTitle:title titleColorForNormal:titleColorForNormal titleColorForHighlighted:titleColorForHighlighted titleColorForSelected:titleColorForSelected titleColorForDisabled:nil iconForNormal:iconForNormal iconForHighlighted:iconForHighlighted iconForSelected:iconForSelected iconForDisabled:nil];
}



- (void)addDockItemWithTitle:(NSString *)title titleColorForNormal:(UIColor*)titleColorForNormal titleColorForHighlighted:(UIColor*)titleColorForHighlighted titleColorForSelected:(UIColor*)titleColorForSelected titleColorForDisabled:(UIColor*)titleColorForDisabled iconForNormal:(NSString*)iconForNormal iconForHighlighted:(NSString *)iconForHighlighted iconForSelected:(NSString *)iconForSelected iconForDisabled:(NSString*)iconForDisabled {
    
    // 1.创建item
    DockItem *item = [[DockItem alloc] init];
    // 文字
    [item setTitle:title forState:UIControlStateNormal];
    if(titleColorForNormal)
        [item setTitleColor:titleColorForNormal forState:UIControlStateNormal];
    if(titleColorForHighlighted)
        [item setTitleColor:titleColorForHighlighted forState:UIControlStateHighlighted];
    if(titleColorForSelected)
        [item setTitleColor:titleColorForSelected forState:UIControlStateSelected];
    if(titleColorForDisabled)
        [item setTitleColor:titleColorForDisabled forState:UIControlStateDisabled];
    // 图标
    if(iconForNormal)
        [item setImage:[UIImage imageNamed:iconForNormal] forState:UIControlStateNormal];
    if(iconForHighlighted)
        [item setImage:[UIImage imageNamed:iconForHighlighted] forState:UIControlStateHighlighted];
    if(iconForSelected)
        [item setImage:[UIImage imageNamed:iconForSelected] forState:UIControlStateSelected];
    if(iconForDisabled)
        [item setImage:[UIImage imageNamed:iconForDisabled] forState:UIControlStateDisabled];
    // 添加一个选项卡
    [self addItem:item];
    
    
}

#pragma mark 添加一个选项卡
- (void)addItem:(UIButton*)item  {
    // 监听item的点击
    [item addTarget:self action:@selector(pressedItem:) forControlEvents:UIControlEventTouchUpInside];
    [_items addObject:item];
    // 2.添加item
    [self addSubview:item];
    // 3.调整所有item的frame
    [self resetItemFrame];
}



-(void)willMoveToWindow:(UIWindow *)newWindow {
    [self resetItemFrame];
}

#pragma mark 调整所有item frame
- (void)resetItemFrame {
    NSInteger count = _items.count;

    CGFloat height = self.frame.size.height; // 高度
    CGFloat width = self.frame.size.width / count; // 宽度
    for (int i = 0; i<count; i++) {
        UIButton *dockItem = _items[i];
        dockItem.tag = i; // 绑定标记
        dockItem.frame = CGRectMake(width * i, 0, width, height);
    }
}



-(NSArray *)items {
    return _items;
}

-(void)setSelectedIndex:(NSInteger)index {
    self.selectedItem = _items[index];
}

-(void)setSelectedItem:(UIButton *)item {

    if ([_delegate respondsToSelector:@selector(dock:didSelectItemFrom:to:)]) {
        [_delegate dock:self didSelectItemFrom:_selectedItem to:item];
    }
    _selectedItem.selected = NO;
    item.selected = YES;
    _selectedItem = item;
    
    
    NSInteger index = [_items indexOfObject:item];
    if ([_delegate respondsToSelector:@selector(dock:didSelectIndexFrom:to:)]) {
        [_delegate dock:self didSelectIndexFrom:_selectedIndex to:index];
    }
    _selectedIndex = index;
}


-(void)pressedIndex:(NSInteger)index {
    [self pressedItem:_items[index]];
}

-(void)pressedItem:(UIButton*)item {
    if ([_delegate respondsToSelector:@selector(dock:didPressedItem:)]) {
     [_delegate dock:self didPressedItem:item];
    }
    
    NSInteger index = [_items indexOfObject:item];
    if ([_delegate respondsToSelector:@selector(dock:didPressedIndex:)]) {
        [_delegate dock:self didPressedIndex:index];
    }
    
    if(_itemSelectable && _selectedItem!=item) {
        [self setSelectedItem:item];
    }
}



@end